//
//  LoginEmailViewController.swift
//  gamlify
//
//  Created by 朱西强 on 2024/12/11.
//

import UIKit

class LoginEmailViewController: UIViewController {
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        setupTextFieldDelegates()
    }
    
    private func setUpUI () {
        // 设置内容pading
        emailTextField.layer.borderWidth = 1
        emailTextField.layer.borderColor = UIColor.systemRed.cgColor
        emailTextField.layer.cornerRadius = 8
        emailTextField.layer.masksToBounds = true
    }
    
    // MARK: - Text Field Delegates
    private func setupTextFieldDelegates() {
        emailTextField.delegate = self
        passwordTextField.delegate = self
    }
    
    @IBAction func login(_ sender: Any) {
        print("login")
    }
    
    @IBAction func forgot(_ sender: Any) {
        print("forgot password")
    }
    
    @IBAction func loginWithGoogle(_ sender: Any) {
        print("login with google")
    }
    
    @IBAction func loginWithX(_ sender: Any) {
        print("login with x")
    }
    
    @IBAction func loginWithFacebook(_ sender: Any) {
        print("login with facebook")
    }
    
    @IBAction func loginWithInstagram(_ sender: Any) {
       print("login with instagram")
    }
    
    @IBAction func changeSecureTextEntry(_ sender: UIButton) {
        sender.isSelected.toggle()
        passwordTextField.isSecureTextEntry.toggle()
    }

    
}

extension LoginEmailViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = textField.text ?? ""
        guard let stringRange = Range(range, in: currentText) else { return false }
        let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
        
        // Change border color based on text field content
        if updatedText.isEmpty {
            textField.layer.borderColor = UIColor.systemRed.cgColor
        } else {
            textField.layer.borderColor = UIColor.systemGreen.cgColor
        }
        
        return true
    }
}


